<?php
namespace app\common\model\business;

use think\Model;
use think\Db;

/**
 * 订单模型
 * add 添加定单
 * edit 编辑定单
 * getOne 得到一个订单
 */
class Orders extends Model
{
    /**
     * add 添加定单
     * @xiao
     * @DateTime 2016-09-06T06:19:16+0800
     * @param    array              $data 添加数组
     * @return   array              [error_code, error_msg, order_id]
     */
    static public function add($data)
    {
        $row = DB::name('orders')->insertGetId($data);
        if ($row)
        {
           $arr['error_code'] = 0;
           $arr['error_msg'] = "";
           $arr['order_id'] = $row;
        }
        else
        {
           $arr['error_code'] = 1;
           $arr['error_msg'] = "订单添加失败";
        } 
        return $arr;
    }

    /**
     * edit 编辑定单
     * @karl
     * @DateTime 2016-09-06T06:19:16+0800
     * @param    int                   $order_id 订单id
     * @param    array                 $data 编辑数据
     * @return   array                 [error_code, error_msg]                  
     */
    static public function edit($order_id,$data)
    { 
        $where['order_id'] = $order_id;
        $row = Db::name('orders')->where($where)->update($data);
            if ($row) 
            {                
                $arr['error_code'] = 0;
                $arr['error_msg'] = "";
                $arr['data'] = $row;
            }
            else{                 
                $arr['error_code'] = 1;
                $arr['error_msg'] = "编辑订单失败";
            }
            return $arr;
    }
    
    /**
     * getOne 得到一个订单
     * @karl
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $order_id 定单id
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function getOne($order_id)
    {   
        $row = Db::name('orders')->where(['order_id' => $order_id])->find();
        if($row)
        {
            $arr['error_code'] = 0;
            $arr['error_msg'] = "";
            $arr['data'] = $row;
        }
        else
        {
            $arr['error_code'] = 1;
            $arr['error_msg'] = "没有得到订单详情";
        }
        return $arr;

    }

    /**
     * addOpe 添加订单操作 记录
     * @karl
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $order_id 定单id
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function addOpe($data)
    { 
        $res = Db::name('orders_operation')->insertGetId($data);
        if($res)
        {
            $arr['error_code'] = 0;
            $arr['error_msg'] = "";
            $arr['id'] = $res;
        }
        else
        {
            $arr['error_code'] = 1;
            $arr['data'] = '';
            $arr['error_msg'] = "失败";
        }
        return $arr;
    }

    /**
     * getOperations 订单操作列表
     * @karl
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $order_id 定单id
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function getOperations($order_id)
    { 
        $where['order_id'] = $order_id;
        $res = Db::name('orders_operation')->where($where)->select();
        if($res) {
            $arr['error_code'] = 0;
            $arr['error_msg'] = "";
            $arr['data'] = $res;
        } else {
            $arr['error_code'] = 1;
            $arr['data'] = '';
            $arr['error_msg'] = "没有得到订单详情";
        }
        return $arr;
    }

    /**
     * getList 得到订单列表
     * @xiao
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    array              $data [''=>''] 条件
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function getList($data, $page = 1, $page_num = 10)
    {   
        //订单信息
        $where['is_dels'] = 0;
        $row = DB::name('orders')->where($data)->field("order_id,order_sn,order_status,pay_code,create_time,order_amount,shipping_time,shipping_price")->order("order_id desc")->page($page,$page_num)->select();
        if ($row) {
            //订单商品信息
            foreach ($row as $k => $v) {
                $id['order_id'] = $v['order_id'];    
                $row[$k]['data'] = Db::name("orders_goods")->field(["id","goods_name","renew_time","buy_num","shop_price","cover_img","goods_id","comment_id"])->where($id)->select();
            }
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $row;
        } else {
            $result['error_code'] = 1;
            $result['error_msg'] = '得到订单详情失败';
        }
        return $result;
    }

    /**
     * ordersList 得到订单列表
     * @xiaoyajun
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $num 页数
     * @param    array                $where 条件
     * @param    array                $url 参数
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function ordersList($num,$where,$url = [])
    {   
        $where['d.is_dels'] = 0;
        $data = Db::name('orders')->alias('d')->join('users_customers u','u.uid = d.uid','left')->field(["d.order_sn","d.order_status","d.order_amount","d.create_time","d.order_id","d.consignee","u.nickname"])
              ->where($where)->order("d.create_time desc")->paginate($num,false,array('query'=>$url)); 
        if($data){
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $data;
        }
        else{
            $result['error_code'] = 1;
            $result['error_msg'] = '未得到得到订单详情';
        }
        return $result;
    }

    /**
     * isPaid 用户是否支付
     * @xiaoyajun
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $order_id
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function isPaid($order_id)
    {   
        $where['order_id'] = $order_id;
        $where['is_pay'] = 1;
        $res = Db::name('orders')->where($where)->find();
        if($res) {
            $arr['error_code'] = 0;
            $arr['error_msg'] = "";
            $arr['data'] = $res;
        } else {
            $arr['error_code'] = 1;
            $arr['data'] = '';
            $arr['error_msg'] = "没有得到订单详情";
        }
        return $arr;
    }

    /**
     * ordersInfo 得到订单详情
     * @xiaoyajun
     * @DateTime 2016-09-28T22:41:15+0800
     * @param    int                $order_id 
     * @return   array              [error_code, error_msg, data=> [] ]
     */
    static public function ordersInfo($order_id)
    {   
        $where['d.order_id'] = $order_id;
        $where['d.is_dels'] = 0;
        $data = Db::name('orders')
              ->alias('d')
              ->join('users_customers u',' d.uid = u.uid','left')
              ->where($where)
              ->find();
        if ($data) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $data;
            $list = OrdersGoods::getList($data['order_id']);
            if ($list['error_code'] == 0) {
                $result['list'] = $list['data'];
            }else{
                $result['list'] = '';
            }
        }else{
            $return['error_code'] = 1;
            $result['error_msg'] = '得到订单详情失败';
        }
        return $result;
    }

    /**
     * isExpire 虚拟商品，服务是否到期exprie_time
     * @karl
     * @DateTime 2017-04-18T13:12:54+0800
     * @return   boolean                  
     */
    static public function isExpire($order_id)
    {

    }
}

?>